import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { ViewRankRunBase } from "./ViewRankRun.generated";

const { regClass } = Laya;

@regClass()
export class ViewRankRun extends ViewRankRunBase {
    private arrRankList:Array<any> =[];
onAwake(): void {
    this.addEvent();
    this.init();
}
onDestroy(): void {
    this.removeEvent();
}
init(){
    this.createListData();
}
public showView():void{
   this.getRankListInfo();
}
private getRankListInfo(){
    HttpManager.post(HttpAPI.POST_USER_RANKLIST,{
        pageNo:1,
        pageSize:10
    },null).then(this.onGetRankList.bind(this)).catch(this.onGetRankList.bind(this));
    HttpManager.get(HttpAPI.GET_USER_INFO,undefined,undefined).then(this.onGetUserRankInfo.bind(this)).catch(this.onGetUserRankInfo.bind(this));
}
private onGetUserRankInfo(data:any){
    if(data &&data.success)
    {
        const result=data.result
        this.PlayerRank.text=result.rankNum;
        this.PlayerRanking.skin="atlas/UI/Rank/phb_icon_no"+result.rankNum+"@1x.png";
        this.Avatar.skin=result.avatar;
        this.AvatarBg.skin="atlas/UI/Rank/phb_txframe_no"+result.rankNum+"@1x.png"
        this.PlayerName.text=result.name;
        this.PlayerScore.text=result.integral;
    }
}
private onGetRankList(data:any){
    this.arrRankList=[];
    if(data &&data.success)
    {
        const result =data.result.records;
        
        for(let item of result)
        {
            this.arrRankList.push({UserName:item.name,Score:item.integral,Avatar:item.avatar})
        }
    }
    if(this.arrRankList.length==0)
    {
        this.List.visible=false;
    }
    else
    {
        this.List.visible=true;
        this.onupdateRankList();
    }
    this.visible=true;
}
addEvent(){
    this.BtnClose.on(Laya.Event.CLICK,this,this.OnClickBtnClose);

}
removeEvent(){
    this.BtnClose.off(Laya.Event.CLICK,this,this.OnClickBtnClose);
   
}
createListData():any{
   this.List.renderHandler = Laya.Handler.create(this, this.onListRender, undefined, false);
   this.List.array= this.arrRankList;
}
onupdateRankList(){
    this.List.array=this.arrRankList;
    this.List.repeatY=this.arrRankList.length;
    this.List.refresh();
    this.List.scrollTo(0);
}
onListRender(item: Laya.Box, index: number) {
    console.log("ViewRankRun onListRender",index,"<==>",item,"<==>");
    // 如果当前索引不在数据源可索引范围,则跳出
    if (index > this.List.array.length || index < 0) return;  
    let score=item.getChildByName("RankBG").getChildByName("ScoreBg").getChildByName("Score") as Laya.Label;
    let avatar=item.getChildByName("RankBG").getChildByName("AvatarBg").getChildByName("Avatar") as Laya.Image;
    let ranking=item.getChildByName("RankBG").getChildByName("Ranking") as Laya.Image;
    let rank=item.getChildByName("RankBG").getChildByName("Rank") as Laya.Label;
    let txt = item.getChildByName("RankBG").getChildByName("UserName") as Laya.Label;
    let avatarBg=item.getChildByName("RankBG").getChildByName("AvatarBg") as Laya.Image;
    const dataList =this.List.array[index];
    txt.text=dataList.UserName;
    score.text=dataList.Score;
    avatar.skin=dataList.Avatar;
    ranking.skin="atlas/UI/Rank/phb_icon_no"+(index+1)+"@1x.png";
    rank.text=(index+1).toString();
    avatarBg.skin="atlas/UI/Rank/phb_txframe_no"+(index+1)+"@1x.png"
    
}
OnClickBtnClose(){
    Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
    this.visible=false;
}

}

